import {
  getToken
} from '../../../utils/getToken'
import {
  formateDate
} from '../../../utils/date'
Page({
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    // 房屋信息
    houseInfo: '',
    houseId: '',
    name: '',
    gender: '',
    mobile: '',
    visitDate: '',
    currentDate: Date.now(),
    maxDate: Date.now() + 24 * 60 * 60 * 1000 * 3
  },
  onLoad() {
    this.getHouseList()
  },
  // 访问日期
  // 确定
  handleDateConfirm(e) {
    console.log(e);
    this.setData({
      visitDate: formateDate(e.detail)
    })
    this.setData({
      dateLayerVisible: false
    })
  },
  // 取消
  handleDateCancel() {
    this.setData({
      dateLayerVisible: false
    })
  },
  // 点击房屋信息进行赋值
  handleHouseSelect(e) {
    // console.log(e);
    this.setData({
      houseId: e.detail.id,
      houseInfo: e.detail.name
    })
  },
  // 获取房屋信息
  getHouseList() {
    wx.request({
      url: 'https://live-api.itheima.net/house',
      method: 'GET',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        if (res.data.code !== 10000) {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        console.log(res);
        this.setData({
          houseList: res.data.data
        })
      }
    })
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  // 表单验证函数
  validateVisitData(data) {
    console.log('开始校验', data)
    // 房屋信息校验
    if (!data.houseId.trim()) {
      wx.showToast({
        title: '请输入访问房屋',
        icon: 'none'
      })
      return false
    }
    // 访客姓名校验
    if (!data.name.trim()) {
      wx.showToast({
        title: '请输入访客姓名',
        icon: 'none'
      })
      return false
    }
    // 访客手机号校验
    const mobileReg = /^[1][3-8][0-9]{9}$/
    if (!mobileReg.test(data.mobile.trim())) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return false
    }
    // 到访日期校验
    if (!data.visitDate.trim()) {
      wx.showToast({
        title: '请选择到访日期',
        icon: 'none'
      })
      return false
    }
    // 校验通过
    console.log('校验通过')
    return true
  },
  // 点击提交
  goPassport() {
    console.log(this.data);
    if (this.validateVisitData) {
      wx.request({
        url: 'https://live-api.itheima.net/visitor',
        method: 'POST',
        data: {
          houseId: this.data.houseId,
          name: this.data.name,
          gender: this.data.gender,
          mobile: this.data.mobile,
          visitDate: this.data.visitDate
        },
        header: {
          Authorization: 'Bearer ' + getToken()
        },
        success: (res) => {
          // console.log('成功', res);
          if (this.data.code !== 10000) {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
          wx.reLaunch({
            url: `/visitor_pkg/pages/passport/index?id=${res.data.data.id}`,
          })
        }
      })
    }
  },
})